#include <string.h>  
#include <stdio.h>  
#include <assert.h>  
#include <math.h>  
#include "dominion.h"  
#include "dominion_helpers.h"  
#include "rngs.h" 

void checkVillage( struct gameState *pre, int handPos, int p ) {
  int r, choice1, choice2, choice3;
  struct gameState post;

  memcpy(&post, pre, sizeof(struct gameState));
  SelectStream(2);
  PutSeed(3);  
  
  choice1 = floor(Random()*256);
  choice2 = floor(Random()*256);
  choice3 = floor(Random()*256);


  r = cardEffect(village, choice1, choice2, choice3, &post, handPos, 0);
  assert( r == 0 );

  drawCard(p, pre);
  printf("Handcount expected: %d, Handcount: %d\n", pre->handCount[p], post.handCount[p] + 1);
  assert( pre->handCount[p] == post.handCount[p] + 1 );
  
  pre->numActions = pre->numActions + 2;
  printf("Num actions expected: %d, Num actions: %d\n", pre->numActions, post.numActions);
  assert( pre->numActions == post.numActions );

  discardCard(handPos, p, pre, 0);
  
  printf("Checking total game state... ");
  assert(memcmp(&post, pre, sizeof(struct gameState)) == 0 );
  printf("PASSED!\n");
}  



int main() {

  int i, k, n, handPos, p, numPlayers;

  struct gameState g;

  PutSeed(3);
  SelectStream(2);

  for(n=0; n<2000; n++) {
    for(i=0; i<sizeof(struct gameState); i++) 
      ((char *)&g)[i] = floor(Random()*256);
    
    numPlayers = floor(Random()*MAX_PLAYERS);
    p = floor(Random()*numPlayers);

    g.deckCount[p] = floor(Random()*MAX_DECK);
    g.discardCount[p] = floor(Random()*MAX_DECK);
    g.handCount[p] = floor(Random()*MAX_HAND);
	for(k=0; k<g.playedCardCount; k++)
		g.playedCards[k] = floor(Random()*MAX_DECK);
    g.numActions = 0;
    g.whoseTurn = p;
    g.numPlayers = numPlayers;
    handPos = floor(Random()*g.handCount[p]);    

    checkVillage( &g, handPos, p );
  }

  printf("All tests PASSED!\n");
  
  return 0;

}